package cn.edu.swpu.cins.u_chain.async.handler;

import cn.edu.swpu.cins.u_chain.Exception.user.UserException;
import cn.edu.swpu.cins.u_chain.async.EventHandler;
import cn.edu.swpu.cins.u_chain.async.EventModel;
import cn.edu.swpu.cins.u_chain.async.EventType;
import cn.edu.swpu.cins.u_chain.frontend.service.user.MailService;
import cn.edu.swpu.cins.u_chain.utils.JedisAdapter;
import cn.edu.swpu.cins.u_chain.utils.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class MailHandler implements EventHandler {

    private static final Logger logger = LoggerFactory.getLogger(EventHandler.class);
    @Autowired
    MailService mailService;
    @Autowired
    JedisAdapter jedisAdapter;



    @Override
    public void doHandle(EventModel model) throws Exception {
            String status=model.getExts().get("status");
            String verifyCode;
            switch (status) {
                case "confirmMail" :
                    verifyCode = mailService.sendSimpleMail(model.getExts().get("mail"),status);
                    jedisAdapter.setex(RedisKey.getuChainMailConfirm(Integer.parseInt(model.getExts().get("userId")),model.getExts().get("mail")),600,verifyCode);
                    break;
                case "changeMail" :
                    verifyCode = mailService.sendSimpleMail(model.getExts().get("mail"),status);
                    jedisAdapter.setex(RedisKey.getuChainChangeMail(Integer.parseInt(model.getExts().get("userId")),model.getExts().get("mail")),600,verifyCode);
                    break;

                case "repeatCheckMail":
                    verifyCode = mailService.sendSimpleMail(model.getExts().get("mail"),status);
                    jedisAdapter.setex(RedisKey.getuChainChangeMail(Integer.parseInt(model.getExts().get("userId")),model.getExts().get("mail")),600,verifyCode);
                    break;

                default:
                    throw new UserException("服务器内部异常");
        }
    }

    @Override
    public List<EventType> getSupportEventTypes() {
        return Arrays.asList(EventType.MAIL);
    }


}
